//
//  CommunityViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/27.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "CommunityViewController.h"
#import "CommendViewController.h"
#import "AttentionViewController.h"
#import "MustNewViewController.h"
#import "UIView+Frame.h"
#import "HX_Macro.h"
@interface CommunityViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *arrTitle;
@property (nonatomic, strong) UIView *titleView;
//底部红色的view
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *selectBtn;
/**
 *  滚动视图
 */
@property (nonatomic, strong) UIScrollView *contentView;
/**
 *  添加视图
 */
@property (nonatomic, strong) UIViewController *showView;
@end

@implementation CommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrTitle = [NSArray array];
    self.arrTitle = @[@"关注", @"推荐", @"最新"];
    [self creatLeftBtn];
    [self addChildVIewController];
    [self creatTitle];
    [self creatTitleBtn];
    [self creatContentView];
}
- (void)creatLeftBtn {
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    
    right.HX_width = 35;
    right.HX_height = 35;
    right.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    [right setImage:[UIImage imageNamed:@"activity"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}
- (void)addChildVIewController {
    CommendViewController *commend = [[CommendViewController alloc] init];
    AttentionViewController *attention = [[AttentionViewController alloc] init];
    MustNewViewController *mustNew = [[MustNewViewController alloc] init];
    [self addChildViewController:attention];
    [self addChildViewController:commend];
    [self addChildViewController:mustNew];
}
- (void)creatTitle {
    self.titleView = [[UIView alloc] init];
    _titleView.HX_width = 200;
    _titleView.HX_height = 44;
    self.navigationItem.titleView = _titleView;
}
- (void)creatTitleBtn {
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor redColor];
    lineView.HX_height = 3;
    lineView.HX_y = _titleView.HX_height - lineView.HX_height;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1000 + i;
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
        btn.HX_x = i * _titleView.HX_width / self.childViewControllers.count;
        btn.HX_y = 0;
        btn.HX_width = _titleView.HX_width / self.childViewControllers.count;
        btn.HX_height = _titleView.HX_height - 3;
        [btn setTitle:_arrTitle[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(chilk:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:btn];
        [btn.titleLabel sizeToFit];
        if (i == 0) {
            [self chilk:btn];
            lineView.HX_width = btn.titleLabel.HX_width;
            lineView.centerX = btn.centerX;
        }
    }
    [self.titleView addSubview:lineView];
    self.lineView = lineView;
}
- (void)chilk:(UIButton *)btn {
    self.selectBtn.enabled = YES;
    btn.enabled = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.HX_width = btn.titleLabel.HX_width;
        self.lineView.centerX = btn.centerX;
    }];
    self.selectBtn = btn;
    NSInteger i = btn.tag - 1000;
    CGFloat index = i * SCREEN_WIDTH;
    [self.contentView setContentOffset:CGPointMake(index, 0) animated:YES];
}
- (void)creatContentView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.delegate = self;
    contentView.frame = self.view.bounds;
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    contentView.backgroundColor = [UIColor orangeColor];
    self.contentView = contentView;
    [self.view insertSubview:contentView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:contentView];
}
/**
 *  将要减速
 *
 *  @param scrollView 滚动视图
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self chilk:_titleView.subviews[i]];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.showView = self.childViewControllers[i];
    _showView.view.frame = self.view.bounds;
    _showView.view.HX_x = i * SCREEN_WIDTH;
    [_contentView addSubview:_showView.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
